package in.muton.edushare.pages.curricula;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.deptpages.DeptBatches;
import in.muton.edushare.pages.univ.UnivBatches;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class ProgramList extends EdusharePanel {
	private static final long serialVersionUID = 932661692908628839L;

	public ProgramList(String id, EduShareBasePage page, Universities univ) {
		super(id, page);
		pageOwner = UNIV_PAGE;
		this.univ = new CayenneModel<Universities>(univ);
		init();
	}

	public ProgramList(String id, EduShareBasePage page, Departments dept) {
		super(id, page);
		pageOwner = DEPT_PAGE;
		this.dept = new CayenneModel<Departments>(dept);
		init();
	}

	private void init() {
		Form<Void> f = new Form<Void>("form");
		add(f);
		f.add(new Button("button") {
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				if (pageOwner == DEPT_PAGE) {
					Departments d = dept.getObject();
					params.put("0", new DepartmentsDAO(getORMInterface())
							.getParent(d).getUniqueName());
					params.put("1", "0");
					params.put("2", Integer.toString(d.getDepartmentId()));
				} else {
					Universities u = (Universities) univ.getObject();
					params.put("0", u.getUniqueName());
				}
				setResponsePage(NewCurriculaPage.class, params);
			}

			@Override
			public boolean isVisible() {
				if (pageOwner == DEPT_PAGE) {
					DeptRoleCheck drc = new DeptRoleCheck(dept.getObject(),
							getLoggedInUser());
					if (drc.isAdmin())
						return true;
				} else {
					UnivRoleCheck urc = new UnivRoleCheck(univ.getObject(),
							getLoggedInUser());
					if (urc.isAdmin())
						return true;
				}
				return false;
			}

			private static final long serialVersionUID = -1853817260280015167L;
		});

		add(new ListView<IModel<Curricula>>("programLV", new CurrModel()) {
			private static final long serialVersionUID = -3315567579487366692L;

			@Override
			protected void populateItem(final ListItem<IModel<Curricula>> arg0) {
				final Curricula c = arg0.getModelObject().getObject();
				Link<Void> progLink = new Link<Void>("programLink") {
					private static final long serialVersionUID = 1528277442177826102L;

					@Override
					public void onClick() {
						Curricula c = arg0.getModelObject().getObject();
						PageParameters params = new PageParameters();
						params.put("0", c.getCurriculaId().toString());
						setResponsePage(CurriculaSummary.class, params);
					}
				};
				
				progLink.add(new Label("programName", c.getCurriculaName()));
				arg0.add(progLink);

				arg0.add(new Link<Void>("delete") {
					private static final long serialVersionUID = -8614605426252828821L;

					@Override
					public void onClick() {
						final Curricula c = arg0.getModelObject().getObject();
						if (c.getBatches().size() > 0) {
							setError("Cannot remove the program as there are batches attached to it."
									+ "<br/>Please remove the batches before proceeding.");
							return;
						}
						new CurriculumDAO(EduShareSession.get()
								.getSessionORMInterface()).deleteCurricula(arg0
								.getModelObject().getObject());
					}

					@Override
					public boolean isVisible() {
						return isOwner();
					}
				});

				arg0.add(new Link<Void>("batches") {
					private static final long serialVersionUID = 5297561379008180258L;

					@Override
					public void onClick() {
						final Curricula c = arg0.getModelObject().getObject();
						if (pageOwner == DEPT_PAGE) {
							PageParameters p = EntityPageParams.getParams(dept);
							Integer i = p.size();
							p.add(i.toString(), c.getCurriculaId().toString());
							setResponsePage(DeptBatches.class, p);
						} else {
							PageParameters p = EntityPageParams.getParams(univ);
							Integer i = p.size();
							p.add(i.toString(), c.getCurriculaId().toString());
							setResponsePage(UnivBatches.class, p);
						}
					}
				});
			}
		});
	}

	private class CurrModel implements IModel<List<IModel<Curricula>>> {
		private static final long serialVersionUID = 44720848899043317L;

		public List<IModel<Curricula>> getObject() {
			if (pageOwner == DEPT_PAGE)
				return new CayenneList<Curricula>(dept.getObject()
						.getCurriculums()).getObject();
			else
				return new CayenneList<Curricula>(univ.getObject()
						.getCurricula()).getObject();
		}

		public void setObject(List<IModel<Curricula>> arg0) {
		}

		public void detach() {
		}
	}

	private boolean isOwner() {
		if (pageOwner == DEPT_PAGE) {
			DeptRoleCheck drc = new DeptRoleCheck(dept.getObject(),
					getLoggedInUser());
			return drc.isPrimaryAdmin() || drc.isUnivAdmin();
		} else
			return new UnivRoleCheck(univ.getObject(), getLoggedInUser())
					.isAdmin();
	}

	private final int DEPT_PAGE = 1;
	private final int UNIV_PAGE = 2;
	private int pageOwner;
	private IModel<Departments> dept = null;
	private IModel<Universities> univ = null;
}
